package com.zmn.oms.business.impl.order;

import com.google.common.collect.Lists;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.oms.model.dto.order.OrderDTO;
import com.zmn.oms.model.dto.order.zmn.CommonRemarkDTO;
import com.zmn.oms.model.dto.order.zmn.ZsNormalNewOrderDTO;
import com.zmn.oms.model.dto.order.zmn.ZsNormalUpdateOrderDTO;
import com.zmn.oms.model.dto.remark.OrderRemarkDTO;
import com.zmn.oms.model.dto.remark.OrderRemarkDetailDTO;
import com.zmn.oms.business.interfaces.order.OrderProcessBService;
import com.zmn.oms.model.entity.order.Order;
import com.zmn.oms.model.entity.shop.OrderExternalShop;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author sunlife
 * @date:2022/8/15 下午2:37
 * description:
 */
@Service
public class OrderProcessBServiceImpl implements OrderProcessBService {
    @Override
    public void processOrder(ZsNormalNewOrderDTO newOrderDTO, OrderDTO orderDTO) {
        // 处理下单备注
        orderDTO.setCommonRemark(this.getInputCommonRemark(newOrderDTO.getCommonRemarkList(), orderDTO));
    }

    @Override
    public void processOrder(ZsNormalUpdateOrderDTO updateOrderDTO, OrderDTO orderDTO) {
        // 处理下单备注
        orderDTO.setCommonRemark(this.getInputCommonRemark(updateOrderDTO.getCommonRemarkList(), orderDTO));
        // 处理外部店铺
        orderDTO.setOrderExternalShop(this.getOrderExternalShop(updateOrderDTO));
    }

    private OrderExternalShop getOrderExternalShop(ZsNormalUpdateOrderDTO updateOrderDTO){
        if (Objects.isNull(updateOrderDTO.getExternalShopInfo())) {
            return null;
        }
        OrderExternalShop orderExternalShop = new OrderExternalShop();
        orderExternalShop.setOrderId(updateOrderDTO.getOrderId());
        orderExternalShop.setWorkId(updateOrderDTO.getWorkId());
        orderExternalShop.setExternalShopId(updateOrderDTO.getExternalShopInfo().getExternalShopId());
        orderExternalShop.setInternalShopId(updateOrderDTO.getExternalShopInfo().getInternalShopId().longValue());
        orderExternalShop.setInternalShopName(updateOrderDTO.getExternalShopInfo().getInternalShopName());
        orderExternalShop.setInternalBranchShopName(updateOrderDTO.getExternalShopInfo().getInternalBranchShopName());
        orderExternalShop.setCreater(updateOrderDTO.getOperator());

        return orderExternalShop;
    }

    private OrderRemarkDTO getInputCommonRemark(List<CommonRemarkDTO> commonRemarkList, OrderDTO orderDTO) {
        if (CollectionUtil.isNullOrEmpty(commonRemarkList)) {
            return null;
        }

        Order order = orderDTO.getOrder();

        OrderRemarkDTO orderRemarkDTO = new OrderRemarkDTO();

        StringBuilder content = new StringBuilder();

        List<OrderRemarkDetailDTO> detailDTOList = Lists.newArrayListWithCapacity(commonRemarkList.size());

        commonRemarkList.forEach(remark -> {
            content.append(remark.getName()).append("；");

            OrderRemarkDetailDTO detailDTO = new OrderRemarkDetailDTO();
            detailDTO.setMapId(remark.getId());
            detailDTO.setMapName(remark.getName());

            detailDTOList.add(detailDTO);
        });

        orderRemarkDTO.setContent(content.toString());
        orderRemarkDTO.setDetailDTOList(detailDTOList);
        orderRemarkDTO.setOrderId(order.getOrderId());
        orderRemarkDTO.setWorkId(order.getOrderId());
        orderRemarkDTO.setOperator(orderDTO.getOperator());
        orderRemarkDTO.setOperatorId(orderDTO.getOperatorId());

        return orderRemarkDTO;
    }
}
